package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 提取销户
 * @author 
 */
@Controller
@RequestMapping("/txxhtqcontroller")
public class txxhtqcontroller {

	private static final Logger logger = Logger.getLogger(txxhtqcontroller.class);
	Map map  = new HashMap();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request,HttpSession session, HttpServletResponse response , String limit ,String id,String start,String end) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ167");
		//接口参数
		reqMap.put("OpenKey",sessionIn.getKEY());
		BODY b=new BODY();
		b.setGRZH(sessionIn.getGRZH());
		reqMap.put("BODY",b);
		OpenKey opk=new OpenKey();
		String result=opk.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0);
				map.put("msg", "1");
				map.put("data",mx.get("RETDATA"));
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("msg", "1");
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
		}
		return null;
	}
	@RequestMapping("/test1")
	@ResponseBody
	public String entryinterface1(HttpServletRequest request,HttpSession session, HttpServletResponse response , String SJHM,String ZJHM,String dqdm,String dwzh,String grzh,String xhyy,String kh,String bj,String lx,String ye,String yh,String gsdqdm,String dxyz ,String tjsj) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		if(xhyy.equals("退休")){
			Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
			int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH) + 1;
			String Zjhm=ZJHM.toString().substring(6, 10);
			int zjn=Integer.parseInt(Zjhm);
			int a=year-zjn;
			if(sessionIn.getXINGBIE().toString().equals("男")){	
				if(a>59){
					Map reqMap = new HashMap();
					BODY b=new BODY();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ168");
					//接口参数
					if(dqdm!=null && dqdm!="" ){
						b.setDQDM(dqdm);
					}else{
						b.setDQDM("");
					}
					if(dwzh!=null && dwzh!="" ){
						b.setDWZH(dwzh);
					}else{
						b.setDWZH("");
					}
					if(grzh!=null && grzh!="" ){
						b.setGRZH(grzh);
					}else{
						b.setGRZH("");
					}
					b.setCZY("网厅");
					if(xhyy!=null && xhyy!="" ){
						b.setXHYY(xhyy);
					}else{
						b.setXHYY("");
					}
					if(kh!=null && kh!="" ){
						b.setKH(kh);	
					}else{
						b.setKH("");
					}
					if(bj!="" && bj!=null ){
						b.setBJ(bj);
					}else{
						b.setBJ("0");
					}
					if(lx!="" && lx!=null){
						b.setLX(lx);
					}else{
						b.setLX("0");
					}
					
					b.setDXYZM(dxyz);
					if(ye!="" && ye !=null){
						b.setYE(ye);
					}
					
					b.setZJHM(sessionIn.getZJHM());
					b.setSJHM(SJHM);
					b.setYH(yh);
					if(gsdqdm!="" && gsdqdm!=null){
						b.setGSDQDM(gsdqdm);
					}else{
						b.setGSDQDM("");
					}
					reqMap.put("OpenKey", sessionIn.getKEY());
					reqMap.put("BODY",b);
					String  result = opk.getOpenKey(reqMap, session);
					Map retMap = JSONUtil.toMap(result);
					logger.info("YBGJJ168接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							map.put("code", 0);
							map.put("msg", "1");
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}else{
							map.put("code", 1);
							map.put("msg", "1");
							map.put("CW", resMap.get("RETURNDESC").toString());
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
							return null;
						}
					}
				}else{
					map.put("code", 1);
					map.put("msg", "1");
					map.put("CW", "您的年龄未满足退休提取");
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
					return null;
				}
			}else if(sessionIn.getXINGBIE().toString().equals("女")){
				if(a>54){
					Map reqMap = new HashMap();
					BODY b=new BODY();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ168");
					//接口参数
					if(dqdm!=null && dqdm!="" ){
						b.setDQDM(dqdm);
					}else{
						b.setDQDM("");
					}
					if(dwzh!=null && dwzh!="" ){
						b.setDWZH(dwzh);
					}else{
						b.setDWZH("");
					}
					if(grzh!=null && grzh!="" ){
						b.setGRZH(grzh);
					}else{
						b.setGRZH("");
					}
					b.setCZY("网厅");
					if(xhyy!=null && xhyy!="" ){
						b.setXHYY(xhyy);
					}else{
						b.setXHYY("");
					}
					if(kh!=null && kh!="" ){
						b.setKH(kh);	
					}else{
						b.setKH("");
					}
					if(bj!="" && bj!=null ){
						b.setBJ(bj);
					}else{
						b.setBJ("0");
					}
					if(lx!="" && lx!=null){
						b.setLX(lx);
					}else{
						b.setLX("0");
					}
					
					b.setDXYZM(dxyz);
					if(ye!="" && ye !=null){
						b.setYE(ye);
					}
					
					b.setZJHM(sessionIn.getZJHM());
					b.setSJHM(SJHM);
					b.setYH(yh);
					if(gsdqdm!="" && gsdqdm!=null){
						b.setGSDQDM(gsdqdm);
					}else{
						b.setGSDQDM("");
					}
					reqMap.put("OpenKey", sessionIn.getKEY());
					reqMap.put("BODY",b);
					String  result = opk.getOpenKey(reqMap, session);
					Map retMap = JSONUtil.toMap(result);
					logger.info("YBGJJ168接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							map.put("code", 0);
							map.put("msg", "1");
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}else{
							map.put("code", 1);
							map.put("msg", "1");
							map.put("CW", resMap.get("RETURNDESC").toString());
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
							return null;
						}
					}
				}else{
					map.put("code", 1);
					map.put("msg", "1");
					map.put("CW", "您的年龄未满足退休提取");
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
					return null;
				}
			}
		}else if(xhyy.equals("离职")){
			 String temp_str="";     
			  
					Map reqMap = new HashMap();
					BODY b=new BODY();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ168");
					//接口参数
					if(dqdm!=null && dqdm!="" ){
						b.setDQDM(dqdm);
					}else{
						b.setDQDM("");
					}
					if(dwzh!=null && dwzh!="" ){
						b.setDWZH(dwzh);
					}else{
						b.setDWZH("");
					}
					if(grzh!=null && grzh!="" ){
						b.setGRZH(grzh);
					}else{
						b.setGRZH("");
					}
					b.setCZY("网厅");
					if(xhyy!=null && xhyy!="" ){
						b.setXHYY(xhyy);
					}else{
						b.setXHYY("");
					}
					if(kh!=null && kh!="" ){
						b.setKH(kh);	
					}else{
						b.setKH("");
					}
					if(bj!="" && bj!=null ){
						b.setBJ(bj);
					}else{
						b.setBJ("0");
					}
					if(lx!="" && lx!=null){
						b.setLX(lx);
					}else{
						b.setLX("0");
					}
					
					b.setDXYZM(dxyz);
					if(ye!="" && ye !=null){
						b.setYE(ye);
					}
					
					b.setZJHM(sessionIn.getZJHM());
					b.setSJHM(SJHM);
					b.setYH(yh);
					if(gsdqdm!="" && gsdqdm!=null){
						b.setGSDQDM(gsdqdm);
					}else{
						b.setGSDQDM("");
					}
					reqMap.put("OpenKey", sessionIn.getKEY());
					reqMap.put("BODY",b);
					String  result = opk.getOpenKey(reqMap, session);
					Map retMap = JSONUtil.toMap(result);
					logger.info("YBGJJ168接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							map.put("msg", "1");
							map.put("code", 0);
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}else{
							map.put("code", 1);
							map.put("msg", "1");
							map.put("CW", resMap.get("RETURNDESC").toString());
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
							return null;
						}
					}
				}
		return null;
	}
}
